//
//  LoginVC.swift
//  RPay
//
//  Created by Charlie Liao on 2017/6/27.
//  Copyright © 2017年 Charlie. All rights reserved.
//

class LoginVC: BaseVC, CTAPIManagerParamSource, CTAPIManagerCallBackDelegate {
    
    
    // MARK: - Init
    convenience init() {
        self.init(nibName: "LoginView", bundle: nil)
    }
    
    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController!.setNavigationBarHidden(true, animated: animated)
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        navigationController!.setNavigationBarHidden(false, animated: animated)
        super.viewWillDisappear(animated)
    }
    
    // MARK: - CTAPIManagerParamSource
    
    func params(forApi manager: CTAPIBaseManager!) -> [AnyHashable : Any]!
    {
        if manager == _codeApi {// 获取验证码
            return [kApiParamUserName: userTF.text!]
        }
        else if manager == _loginApi { // 登录
            return [kApiParamUserName: userTF.text!, kApiParamUserPwd: pwdTF.text!, kApiParamVerifyCode: codeTF.text!]
        }
        
        return [AnyHashable : Any]()
    }
    
    // MARK: - CTAPIManagerCallBackDelegate
    
    func managerCallAPIDidSuccess(_ manager: CTAPIBaseManager!)
    {
        hideLoading()
        if manager == _codeApi { // 获取验证码
            _codeApi = nil
        }
        else if manager == _loginApi { // 登录
            _loginApi = nil
            let data = manager.fetchData(with: nil) as! [String:Any]
            let login = data["content"]
            let user = LoginUser(content: login as! [String : Any])
            UserShare.shared.loginUser = user

            // 登录成功跳转页面
            IFShared().pushTabbarVC()
        }
    }
    
    func managerCallAPIDidFailed(_ manager: CTAPIBaseManager!) {
        hideLoading()
        alert(manager.errorMessage)
        if manager == _codeApi {
            _codeApi = nil
        } else if manager == _loginApi {
            _loginApi = nil
        }
    }
    
    // MARK: - Event Respond
    
    /// 点击获取验证码按钮
    @IBAction func tapGetCodeBtn() {
        if userTF.text!.isEmpty {
            alert(LocalStr("请输入登录账号"))
            return
        }
        showLoading()
        codeApi.loadData()
    }
    
    /// 点击忘记密码按钮
    @IBAction func tapForgetBtn() {
        let vc = ForgetPwdVC()
        navigationController!.pushViewController(vc, animated: true)
    }
    
    ///  点击登录按钮
    @IBAction func tapLoginBtn() {
        if userTF.text!.isEmpty {
            alert(LocalStr("请输入登录账号"))
            return
        }
        if pwdTF.text!.isEmpty {
            alert(LocalStr("请输入登录密码"))
            return
        }
        if pwdTF.text!.isEmpty {
            alert(LocalStr("请输入验证码"))
            return
        }
        
        showLoading()
        loginApi.loadData()
    }
    
    // MARK: - Get
    
    @IBOutlet private var userTF: UITextField!
    @IBOutlet private var pwdTF: UITextField!
    @IBOutlet private var codeTF: UITextField!
    
    private var _codeApi: GetCodeApiManager?
    private var codeApi: GetCodeApiManager {
        if _codeApi == nil {
            _codeApi = GetCodeApiManager()
            _codeApi!.paramSource = self
            _codeApi!.delegate = self
        }
        return _codeApi!
    }
    
    private var _loginApi: LoginApiManager?
    private var loginApi: LoginApiManager {
        if _loginApi == nil {
            _loginApi = LoginApiManager()
            _loginApi!.paramSource = self
            _loginApi!.delegate = self
        }
        return _loginApi!
    }
}
